package com.qq.mobilemanager.activity;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qq.mobilesafe.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EnterPwdActivity extends Activity implements OnClickListener {
	@ViewInject(R.id.et_pwd)
	private EditText et_pwd;

	@ViewInject(R.id.btn_0)
	private Button btn_0;
	@ViewInject(R.id.btn_1)
	private Button btn_1;
	@ViewInject(R.id.btn_2)
	private Button btn_2;
	@ViewInject(R.id.btn_3)
	private Button btn_3;
	@ViewInject(R.id.btn_4)
	private Button btn_4;
	@ViewInject(R.id.btn_5)
	private Button btn_5;
	@ViewInject(R.id.btn_6)
	private Button btn_6;
	@ViewInject(R.id.btn_7)
	private Button btn_7;
	@ViewInject(R.id.btn_8)
	private Button btn_8;
	@ViewInject(R.id.btn_9)
	private Button btn_9;
	@ViewInject(R.id.btn_clear)
	private Button btn_clear;
	@ViewInject(R.id.btn_delete)
	private Button btn_delete;
	@ViewInject(R.id.btn_ok)
	private Button btn_ok;

	private String packname;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
	}

	private void initView() {
		setContentView(R.layout.activity_set_pwd);
		ViewUtils.inject(this);
		Intent intent = getIntent();
		if (intent != null) {
			packname = intent.getStringExtra("packagename");
		}

		et_pwd.setInputType(InputType.TYPE_NULL);
		btn_ok.setOnClickListener(this);
		btn_clear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				et_pwd.setText("");
			}
		});

		btn_delete.setOnClickListener(new OnClickListener() {

			private String str;

			@Override
			public void onClick(View v) {
				str = et_pwd.getText().toString();
				if (str.length() == 0) {
					return;
				}
				et_pwd.setText(str.substring(0, (str.length() - 1)));
			}
		});

		btn_0.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String str = et_pwd.getText().toString();
				et_pwd.setText(str + btn_0.getText().toString());
			}
		});
		btn_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String str = et_pwd.getText().toString();
				et_pwd.setText(str + btn_1.getText().toString());
			}
		});
		btn_2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String str = et_pwd.getText().toString();
				et_pwd.setText(str + btn_2.getText().toString());
			}
		});
		btn_3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String str = et_pwd.getText().toString();
				et_pwd.setText(str + btn_3.getText().toString());
			}
		});
		btn_4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String str = et_pwd.getText().toString();
				et_pwd.setText(str + btn_4.getText().toString());
			}
		});
		btn_5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String str = et_pwd.getText().toString();
				et_pwd.setText(str + btn_5.getText().toString());
			}
		});
		btn_6.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String str = et_pwd.getText().toString();
				et_pwd.setText(str + btn_6.getText().toString());
			}
		});
		btn_7.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String str = et_pwd.getText().toString();
				et_pwd.setText(str + btn_7.getText().toString());
			}
		});
		btn_8.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String str = et_pwd.getText().toString();
				et_pwd.setText(str + btn_8.getText().toString());
			}
		});
		btn_9.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String str = et_pwd.getText().toString();
				et_pwd.setText(str + btn_9.getText().toString());
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_ok:

			String result = et_pwd.getText().toString();
			if ("123".equals(result)) {
				Intent intent = new Intent();
				intent.setAction("com.qq.mobileguard.stopprotect");
				intent.putExtra("packagename", packname);

				sendBroadcast(intent);
				finish();
			} else {
				Toast.makeText(EnterPwdActivity.this, "密码错误", 0).show();
			}

			break;
		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		// 当用户输入后退健 的时候。我们进入到桌面
		Intent intent = new Intent();
		intent.setAction("android.intent.action.MAIN");
		intent.addCategory("android.intent.category.HOME");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addCategory("android.intent.category.MONKEY");
		startActivity(intent);
	}

}
